﻿
using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    [AddComponentMenu("CGFramework/CText")]
    [RequireComponent(typeof(CTextEffect))]
    public class CText : Text
    {
        [System.Serializable]
        public class Outline
        {
            public Color color = new Color(0.6f, 0.2f, 0);
            public float size = 5;
        }
        [System.Serializable]
        public class Shadow
        {
            public Color color = new Color(0, 0, 0, 0.5f);
            public Vector2 offset = Vector2.zero;
        }

        //渐变
        public enum GradientColor { None, Horizontal, Vertical }
        public GradientColor gradientColor = GradientColor.None;
        public Color32 begin = Color.white;
        public Color32 end = new Color(1, 1, 0);
        //描边
        public Outline[] outlines = new Outline[0];
        //阴影
        public Shadow[] shadows = new Shadow[0];

        protected override void Start()
        {
            base.Start();
            CTextEffect textEffect = GetComponent<CTextEffect>();
            textEffect.enabled = true;
            textEffect.Refresh();
        }

#if UNITY_EDITOR
        [UnityEditor.MenuItem("CGFramework/创建 CText")]
        [UnityEditor.MenuItem("GameObject/CGFramework/创建 CText", priority = -2200)]
        static void Create()
        {
            GameObject go = new GameObject("Text");
            go.layer = LayerMask.NameToLayer("UI");
            go.transform.SetParent(UnityEditor.Selection.activeGameObject.transform, false);
            CText cText = go.AddComponent<CText>();
            cText.alignment = TextAnchor.MiddleCenter;
            cText.horizontalOverflow = HorizontalWrapMode.Overflow;
            cText.verticalOverflow = VerticalWrapMode.Overflow;
            cText.rectTransform.sizeDelta = Vector2.zero;
            UnityEditor.EditorUtility.SetDirty(go);
        }


        [UnityEditor.MenuItem("GameObject/CGFramework/Text 转 CText", priority = -2100)]
        public static void ChangeToCText()
        {
            for (int i = 0; i < UnityEditor.Selection.objects.Length; i++)
            {
                GameObject go = UnityEditor.Selection.objects[i] as GameObject;
                string content = "";
                Font font = null;
                FontStyle fontStyle = FontStyle.Normal;
                int fontSize = 0;
                float lineSpace = 0;
                bool supportRichText = true;
                TextAnchor alignment = TextAnchor.MiddleCenter;
                HorizontalWrapMode horizontalOverflow = HorizontalWrapMode.Overflow;
                VerticalWrapMode verticalOverflow = VerticalWrapMode.Overflow;
                Color color = Color.white;
                bool raycastTarget = true;
                Image image = go.GetComponent<Image>();
                if (image)
                {
                    DestroyImmediate(image);
                }
                Text text = go.GetComponent<Text>();
                if (text)
                {
                    content = text.text;
                    font = text.font;
                    fontStyle = text.fontStyle;
                    fontSize = text.fontSize;
                    lineSpace = text.lineSpacing;
                    supportRichText = text.supportRichText;
                    alignment = text.alignment;
                    horizontalOverflow = text.horizontalOverflow;
                    verticalOverflow = text.verticalOverflow;
                    color = text.color;
                    raycastTarget = text.raycastTarget;
                    DestroyImmediate(text);
                }
                CText cText = go.GetComponent<CText>();
                if (!cText) cText = go.AddComponent<CText>();
                cText.text = content;
                if (font) cText.font = font;
                if (fontSize > 0) cText.fontSize = fontSize;
                if (lineSpace > 0) cText.lineSpacing = lineSpace;
                cText.fontStyle = fontStyle;
                cText.supportRichText = supportRichText;
                cText.alignment = alignment;
                cText.horizontalOverflow = horizontalOverflow;
                cText.verticalOverflow = verticalOverflow;
                cText.color = color;
                cText.raycastTarget = raycastTarget;
            }
        }
        
        [UnityEditor.MenuItem("GameObject/CGFramework/CText 转 Text", priority = -2100)]
        public static void ChangeToText()
        {
            for (int i = 0; i < UnityEditor.Selection.objects.Length; i++)
            {
                GameObject go = UnityEditor.Selection.objects[i] as GameObject;
                string content = "";
                Font font = null;
                FontStyle fontStyle = FontStyle.Normal;
                int fontSize = 0;
                float lineSpace = 0;
                bool supportRichText = true;
                TextAnchor alignment = TextAnchor.MiddleCenter;
                HorizontalWrapMode horizontalOverflow = HorizontalWrapMode.Overflow;
                VerticalWrapMode verticalOverflow = VerticalWrapMode.Overflow;
                Color color = Color.white;
                bool raycastTarget = true;
                Image image = go.GetComponent<Image>();
                if (image)
                {
                    DestroyImmediate(image);
                }
                CText cText = go.GetComponent<CText>();
                if (cText)
                {
                    content = cText.text;
                    font = cText.font;
                    fontStyle = cText.fontStyle;
                    fontSize = cText.fontSize;
                    lineSpace = cText.lineSpacing;
                    supportRichText = cText.supportRichText;
                    alignment = cText.alignment;
                    horizontalOverflow = cText.horizontalOverflow;
                    verticalOverflow = cText.verticalOverflow;
                    color = cText.color;
                    raycastTarget = cText.raycastTarget;
                    DestroyImmediate(cText);
                }
                Text text = go.GetComponent<Text>();
                if (!text) text = go.AddComponent<Text>();
                text.text = content;
                if (font) text.font = font;
                if (fontSize > 0) text.fontSize = fontSize;
                if (lineSpace > 0) text.lineSpacing = lineSpace;
                text.fontStyle = fontStyle;
                text.supportRichText = supportRichText;
                text.alignment = alignment;
                text.horizontalOverflow = horizontalOverflow;
                text.verticalOverflow = verticalOverflow;
                text.color = color;
                text.raycastTarget = raycastTarget;
                CTextEffect effect = go.GetComponent<CTextEffect>();
                if (effect)
                {
                    DestroyImmediate(effect);
                }
            }
        }

        [UnityEditor.MenuItem("GameObject/CGFramework/Text 转 Overflow", priority = -2100)]
        public static void ChangeToOverflow()
        {
            for (int i = 0; i < UnityEditor.Selection.objects.Length; i++)
            {
                GameObject go = UnityEditor.Selection.objects[i] as GameObject;
                Text text = go.GetComponent<Text>();
                if (text)
                {
                    text.rectTransform.sizeDelta = Vector2.zero;
                    text.alignment = TextAnchor.MiddleCenter;
                    text.horizontalOverflow = HorizontalWrapMode.Overflow;
                    text.verticalOverflow = VerticalWrapMode.Overflow;
                    text.color = Color.white;
                }
                CText cText = go.GetComponent<CText>();
                if (cText)
                {
                    cText.rectTransform.sizeDelta = Vector2.zero;
                    cText.alignment = TextAnchor.MiddleCenter;
                    cText.horizontalOverflow = HorizontalWrapMode.Overflow;
                    cText.verticalOverflow = VerticalWrapMode.Overflow;
                    cText.color = Color.white;
                }
            }
        }
#endif
    }
}